package CMessage
{
	import Classes.pi_events;
	
	import flash.geom.Rectangle;
	
	public class cm_message extends Object {
		
	    [Embed(source="images/paper.jpg")]
	    [Bindable]
	    public static var c_paper: Class;

		public static var ui: cm_userInterface;
		public static var HEIGHTMSGBOX: Number = 98;
		private var _window	: Object;
		private var _params : Array;
		private var _card	: Object;
		public function cm_message(cardBody: *, params: *) {
			super();
			_params = params;
			_card 	= cardBody;
			_card.app.addEventListener(pi_events.COMPLETEFULLLOAD, doCardLoad);
			ui.app.styles['MSG_window'] = {
				filters: {
					shadow: {
						distance: 1,
						angle: 80,
						blurX: 5,
						blurY: 5,
						color: 0,
						strength: 2
					}					
				}
			}
			
			ui.app.styles['MSG_text'] = {
				margin: 10,
				valign: true,
				text_format: {
					color: 0x444333,
					font: 'arial',
					selectable: false,
					align: 'center',
					wordWrap: true,
					size: 14
				}
			}
		}
		
		public function get asView(): Boolean {
			return ui.app.fullScreen == _card;
		}
		
		public static function decode(text: String): String {
			var tl: int = text.length;
			var arr: Array = text.match(/x[0-9]{1,4}/g);
			if (arr.length > 2) {
				var result: String = '';//String.fromCharCode([1233, 1023]);
				for (var i:int=0;i<arr.length;i++)
					result += String.fromCharCode((arr[i] as String).slice(1));
				return result;
			} else if (text.match(/\%u[A,B,C,D,E,F,0-9]{4}/g).length == int(tl / 6)) 
				return unescape(text);
			else if (text.match(/\%[A,B,C,D,E,F,0-9]{2}/g).length == int(tl / 3)) 
				return decodeURIComponent(text);
			else return text;
		}
		
		public static function encode(text: String): String {
			var result: String = '';
			var count: int = text.length;
			for (var i:int=0; i<count; i++) {
				result += 'x' + text.charCodeAt(i);
			}
			return result;
		}
		
		protected function doCardLoad(e: Object): void {
			if (asView && (_params.length > 1) && (_params[1])) {
				var cbImage: Object = _card.hasOwnProperty('bitmap')?_card.bitmap:_card.Container;
				if (cbImage.y > HEIGHTMSGBOX) cbImage.y -= HEIGHTMSGBOX;
				else cbImage.y = 0;
				var rect: Rectangle = new Rectangle(cbImage.x, cbImage.y + cbImage.height + 5,
													cbImage.width, HEIGHTMSGBOX);
				if (rect.y + rect.height > 726) rect.y = 726 - rect.height;
				_window = new c_message.classes.component(_card.app.layers[2], 
							(760 - rect.width) / 2, rect.y, rect.width, rect.height, 'MSG_window');
				var _mask: Object = new c_message.classes.component(_window, 
							0, 0, rect.width, rect.height, 'white');
				var _paper: Object = new c_paper();
				_window.addChild(_paper);
				_paper.mask = _mask;
				_paper.x = (_window.width - _paper.width) / 2;
				
				var _MSGtext: Object = new c_message.classes.label(_window, 
							0, 3, rect.width, rect.height, 'MSG_text');
				
				_MSGtext.text = decode(_params[1]);
				_window.slideShow();
			}
		}
		
		public function get params(): Object {
			return _params;
		}
		
		public function get editMode(): Boolean {
			return !ui.app.isFullView && (ui.app.editor != null); 
		}
		
		public function dispose(): void {
			if (_window && _window.parent) _window.parent.removeChild(_window);
			_window = null;
			_card.app.removeEventListener(pi_events.COMPLETEFULLLOAD, doCardLoad);
		}
	}
}